package com.xbts.screen.controller.screen;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.xbts.common.core.enums.WorkshopTypeEnum;
import com.xbts.common.core.web.controller.BaseController;
import com.xbts.common.core.web.domain.AjaxResult;
import com.xbts.common.log.annotation.Log;
import com.xbts.common.log.enums.BusinessType;
import com.xbts.common.security.annotation.RequiresPermissions;
import com.xbts.screen.domain.WeScreenConfig;
import com.xbts.screen.domain.WeWorkshopType;
import com.xbts.screen.service.IWeScreenConfigService;
import com.xbts.screen.service.IWeWorkshopTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * 大屏配置Controller
 * 
 * @author xbts
 * @date 2024-09-05
 */
@Api(tags = "大屏配置管理")
@RestController
@RequestMapping("/config")
public class WeScreenConfigController extends BaseController {

    @Value("${spring.datasource.dynamic.datasource.master.url}")
    private String dbcUrl;
    @Value("${spring.datasource.dynamic.datasource.master.username}")
    private String jdbcUser;
    @Value("${spring.datasource.dynamic.datasource.master.password}")
    private String jdbcPassword;

    @Autowired
    private IWeScreenConfigService weScreenConfigService;
    @Autowired
    private IWeWorkshopTypeService weWorkshopTypeService;

    /**
     * 获取大屏配置详细信息
     */
    @ApiOperation("获取大屏配置详细")
    @RequiresPermissions("screen:config:query")
    @GetMapping(value = "/details")
    public AjaxResult getInfo() {
        return success(weScreenConfigService.list().get(0));
    }

    /**
     * 修改大屏配置
     */
    @ApiOperation("更新大屏配置")
    @RequiresPermissions("screen:config:edit")
    @Log(title = "大屏配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeScreenConfig weEnvironmentConfig) {
        return toAjax(weScreenConfigService.updateById(weEnvironmentConfig));
    }


    /**
     * 获取大屏环保信息
     */
    @ApiOperation("获取大屏环保信息")
    @RequiresPermissions("screen:environment:query")
    @GetMapping(value = "/environmentUrl")
    public AjaxResult getEnvironmentUrl() {
        WeScreenConfig weScreenConfig = weScreenConfigService.list().get(0);
        return success((Object)weScreenConfig.getEnvironmentUrl());
    }
    /**
     * 获取大屏宣传信息
     */
    @ApiOperation("获取大屏宣传信息")
    @RequiresPermissions("screen:publicize:query")
    @GetMapping(value = "/publicizeUrl")
    public AjaxResult getPublicizeUrl() {
        WeScreenConfig weScreenConfig = weScreenConfigService.list().get(0);
        return success((Object)weScreenConfig.getPublicizeUrl());
    }
    /**
     * 获取车间对应ERP工序名称
     */
    @ApiOperation("获取车间对应ERP工序名称")
    @RequiresPermissions("screen:workshop_erp_name:query")
    @GetMapping(value = "/workshopErpName")
    public AjaxResult workshopErpName() {
        return success(weWorkshopTypeService.list(new LambdaQueryWrapper<WeWorkshopType>().ne(WeWorkshopType::getId, WorkshopTypeEnum.OVERALL.name())));
    }
    /**
     * 获取车间对应ERP工序名称
     */
    @ApiOperation("获取车间对应ERP工序名称")
    @RequiresPermissions("screen:workshop_erp_name:edit")
    @PutMapping(value = "/workshopErpName")
    public AjaxResult workshopErpName(@RequestBody WeWorkshopType weWorkshopType) {
        weWorkshopType.setType(null);
        weWorkshopTypeService.updateById(weWorkshopType);
        // 更新枚举
        WorkshopTypeEnum.initializeFromDatabase(dbcUrl, jdbcUser, jdbcPassword);
        return success();
    }
}
